package org.ccay.core.xml;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.xml.sax.SAXException;

import org.ccay.core.util.EncodingUtil;

/**
 * xml工具类，创建文档，元素，属性；读，写小型的xml文档等静态方法
 * Created：2013-8-6
 * @author chaowangbang
 * @version:
 */
public class XmlHelper {
	
	private  XmlHelper(){
		
	};
	
	/**
	 * 创建一个元素
	 * @param name
	 * @return
	 */
	public static Element createElement(String name) {
		return DocumentHelper.createElement(name);
	}

	/**
	 * 创建一个元素,并指定内容
	 * @param name
	 * @return
	 */
	public static Element createElement(String name, String text) {
		Element element = DocumentHelper.createElement(name);
		element.setText(text);
		return element;
	}
	
	/**
	 * 创建一个元素,并指定CDATA
	 * @param name
	 * @return
	 */
	public static Element createElementCDATA(String name, String CDATAText) {
		Element element = DocumentHelper.createElement(name);
		element.addCDATA(CDATAText);
		return element;
	}
	
	/**
	 * 创建一个属性
	 * @param owner
	 * @param name
	 * @param value
	 * @return
	 */
	public static Attribute createAttribute(Element owner, String name, String value) {
		return DocumentHelper.createAttribute(owner, name, value);
	}

	/**
	 * 创建一个文档，并指定根元素
	 * @param rootElement
	 * @return
	 */
	public static Document createDocument(Element rootElement) {
		return DocumentHelper.createDocument(rootElement);
	}
	
	/**
	 * 创建一个文档
	 * @return
	 */
	public static Document createDocument() {
		return DocumentHelper.createDocument();
	}
	
	/**
	 * 读取一个xml文件
	 * @param file
	 * @return
	 * @throws DocumentException 
	 */
	public static Document readDocument(InputStream inputStream) throws DocumentException {
		SAXReader reader = new SAXReader();
		Document document = null;
		try {
			reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
		} catch (SAXException e) {
			e.printStackTrace();
		}
		document = reader.read(inputStream);
		return document;
	}
	
	/**
	 * 读取一个xml文件
	 * @param file
	 * @return
	 * @throws DocumentException 
	 */
	public static Document readDocument(File file) throws DocumentException {
		SAXReader reader = new SAXReader();
		Document document = null;
		try {
			reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
		} catch (SAXException e) {
			e.printStackTrace();
		}
		document = reader.read(file);
		return document;
	}
	
	/**
	 * 读取一个xml文件
	 * @param file
	 * @return
	 * @throws DocumentException 
	 */
	public static Document readDocument(String pathname) throws DocumentException {
		SAXReader reader = new SAXReader();
		Document document = null;
		File file = new File(pathname);
		try {
			reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
		} catch (SAXException e) {
			e.printStackTrace();
		}
		document = reader.read(file);
		return document;
	}
	
	/**
	 * 将xml文档写入文件
	 * @param document 文档对象
	 * @param filepath 指定文件的路径
	 * @param codingFormat xml文件头部编码格式声明
	 * @throws IOException 
	 */
	public static void writeDocument(Document document, String filepath) throws IOException {
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding(EncodingUtil.UTF_8);
		XMLWriter writer = null;
		try{
			writer = new XMLWriter(new FileOutputStream(filepath), format);
			writer.write(document);
		}finally{
			if(writer != null){
				writer.close();
			}
		}
	}
	
	/**
	 * 将字符串转换成xml文档对象
	 * @param text
	 * @return
	 * @throws DocumentException 
	 */
	public static Document parseText(String text) throws DocumentException {
		Document doc = null;
			doc = DocumentHelper.parseText(text);
		return doc;
	}
	
	/**
	 * 将一个节点转换成xml文本
	 * @param node
	 * @return
	 */
	public static String asXML(Node node) {
		String str = null;
		if (node != null) {
			str = node.asXML();
		}
		return str;
	}

}
